# clean environment
rm(list=ls())

# load packages
library(ggplot2)
library(openxlsx)

# load data set with chapter length and crime numbers
dat <- openxlsx::read.xlsx("vsberichte_metadata.xlsx", sheet = 1)

# binary varibable indicating whether interior minister is from a center-right party
dat$cr_intmin <- ifelse(dat$intmin_party == "cdu" | dat$intmin_party== "csu" | dat$intmin_party=="fdp", 1, ifelse(dat$intmin_party=="pro", NA, 0))

# create chapter length variable by taking the difference between the page number in which the RWE and LWE chapter ends and begins
dat$rwe_chapter_length <- dat$page_right_end-dat$page_right_start
dat$lwe_chapter_length <- dat$page_left_end-dat$page_left_start

# sum up chapter lengths on the year level, by interior minister partisanship 
#dat_agg <- aggregate(cbind(rwe_chapter_length, lwe_chapter_length, extreme_crime_right, extreme_crime_left)~year+cr_intmin, dat, mean)

# create logged ratio of RWE and LWE chapter lengths
dat$rwe_lwe_chapter_ratio_log <- log(dat$rwe_chapter_length + 0.5) - log(dat$lwe_chapter_length + 0.5)

# logged crime ratio
dat$rwe_lwe_crime_ratio_log <- log(dat$extreme_crime_right + 0.5) - log(dat$extreme_crime_left + 0.5)

# create bias measure (difference between logged chapter length ratio and looged crime ratio)
dat$bias_ratio <- dat$rwe_lwe_chapter_ratio_log - dat$rwe_lwe_crime_ratio_log

# sum up chapter lengths on the year level, by interior minister partisanship 
dat_agg <- aggregate(bias_ratio~year+cr_intmin, dat, mean)

# plot bias measure over time
ggplot(data=dat_agg, aes(x=year, y=bias_ratio, 
                         color=factor(cr_intmin), 
                         fill=factor(cr_intmin),
                         shape=factor(cr_intmin))) + 
  geom_point(size=2.5) + 
  #  stat_smooth() +
  geom_line() +
  geom_hline(yintercept=0, linetype="dashed") +
  theme_classic() +
  theme(legend.position = "bottom", text=element_text(size=16)) +
  scale_color_manual(values=c("firebrick", "dodgerblue"), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  scale_fill_manual(values=c("firebrick", "dodgerblue"), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  scale_shape_manual(values=c(16, 17), name="Interior Minister Party", labels=c("Center-Left", "Center-Right")) +
  ylab("Bias") + xlab("") +
  scale_x_continuous(breaks=seq(1970, 2025, 10))

ggsave("FigD2.pdf", width=7, height=5)
